package com.chat.demo.userservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/group")
public class GroupMessage_Controller {

    @Autowired
    private Message_Broadcast_Service service;

    // 发送群消息
    @PostMapping("/room/send")
    public ResponseEntity<?> sendGroupMessage(@RequestParam int senderId,
                                              @RequestParam int roomId,
                                              @RequestParam String content) {
        if (service.sendGroupMessage(senderId, roomId, content)) {
            return ResponseEntity.ok("群聊消息已发送");
        }
        return ResponseEntity.badRequest().body("发送失败");
    }

    // 获取群历史记录
    @GetMapping("/room/history")
    public ResponseEntity<?> getGroupHistory(@RequestParam int roomId,
                                             @RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "20") int size) {
        return ResponseEntity.ok(service.getGroupHistory(roomId, page, size));
    }

    // 更新状态（例如设置为 read）
    @PostMapping("/status")
    public ResponseEntity<?> updateStatus(@RequestParam int messageId,
                                          @RequestParam String status) {
        if (service.updateMessageStatus(messageId, status)) {
            return ResponseEntity.ok("status updated");
        }
        return ResponseEntity.badRequest().body("update failed");
    }
}
